<?php

class SiteController extends Controller
{

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
		);
	}

	// dashboard
	public function actionIndex()
	{
		$this->render('index', array(
			'count' => count(Yii::app()->pf->getAll()),
			'phpReport' => $this->checkPHP()
		));
	}

	// login
	public function actionLogin()
	{

		$model = new LoginForm();
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if (isset($_POST['LoginForm'])) {
			$model->attributes = $_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate() && $model->login()) {
				$this->redirect(Yii::app()->user->returnUrl);
			}
		}
		$this->layout = '//layouts/simple';
		$this->render('login', array('model' => $model));
	}

	// logout
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	// error page
	public function actionError()
	{
		if (($error = Yii::app()->errorHandler->error) !== null) {
			if (Yii::app()->request->isAjaxRequest) {
				echo $error['message'];
			} else {
				$this->layout = '//layouts/simple';
				$this->render('error', $error);
			}
		}
	}

	/**
	 * @return array
	 */
	private function checkPHP()
	{
		$result = array('error' => array(), 'warning' => array());
		// php version
		if (version_compare(PHP_VERSION, '5.2.0', '<')) {
			$result['error'][] = 'PHP 版本必须高于 5.2.0';
		}
		// extensions: pcre, spl, json
		foreach (array('pcre', 'spl', 'json') as $name) {
			if (!extension_loaded($name)) {
				$result['error'][] = '缺少扩展 ' . self::phpExtLink($name);
			}
		}
		// functions: fsockopen
		foreach (array('fsockopen') as $name) {
			if (!function_exists($name)) {
				$result['error'][] = '缺少函数 ' . self::phpFunLink($name);
			}
		}
		// encoding
		if (!function_exists('mb_convert_encoding') && !function_exists('iconv')) {
			$result['error'][] = '缺少转码扩展 ' . self::phpExtLink('mbstring') . ' 或 ' . self::phpExtLink('iconv');
		}
		// sqlite
		if (!extension_loaded('sqlite') && !extension_loaded('sqlite3') && !extension_loaded('pdo_sqlite')) {
			$result['error'][] = '缺少 ' . self::phpExtLink('sqlite') . ' 扩展';
		}
		// mysql (warning)
		if (!function_exists('mysql_connect') && !class_exists('mysqli') && !extension_loaded('pdo_mysql')) {
			$result['warning'][] = '缺少 ' . self::phpExtLink('mysql') . ' 扩展';
		}
		// cache
		if (!function_exists('apc_fetch') && !function_exists('xcache_get') && !function_exists('eaccelerator_get')) {
			$result['warning'][] = '缺少缓存函数 ' . self::phpFunLink('apc_fetch') . ' 或 ' . self::phpFunLink('xcache_get');
		}
		// result
		if (count($result['warning']) === 0 && count($result['error']) === 0) {
			$result['success'] = array('全部通过');
		}
		return $result;
	}

	private static function phpFunLink($name)
	{
		return CHtml::link($name, 'http://www.php.net/manual/zh/function.' . $name . '.php', array('target' => '_blank'));
	}

	private static function phpExtLink($name)
	{
		return CHtml::link($name, 'http://www.php.net/manual/zh/book.' . $name . '.php', array('target' => '_blank'));
	}
}
